package com.scott.crm.delay.service.impl;

import com.scott.base.utils.AESUtil;
import com.scott.crm.delay.bean.ClazzUserBean;
import com.scott.crm.delay.bean.DelayBean;
import com.scott.crm.delay.dao.ClazzUserDao;
import com.scott.crm.delay.dao.DelayDao;
import com.scott.crm.delay.service.DelayService;
import com.scott.crm.role.bean.RoleBean;
import com.scott.crm.role.bean.UserRoleBean;
import com.scott.crm.role.dao.RoleDao;
import com.scott.crm.role.dao.UserRoleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 方雅浩
 * @date 2021-11-29 16:44:36
 * @Version 1.0
 */
@Service
public class DelaySeviceImpl implements DelayService {

    @Autowired
    private DelayDao dao;

    @Autowired
    private ClazzUserDao clazzUserDao;

    @Autowired
    private UserRoleDao userRoleDao;

    @Autowired
    private RoleDao roleDao;

    @Override
    public int addDelayRecord(DelayBean delayBean) {
        if(delayBean.getToken()==null||delayBean.getUserId()==0||delayBean.getDelayDate()==""){
            return 0;
        }
        String token = delayBean.getToken();
        String userId =null;
        try {
            userId = AESUtil.decrypt(token, AESUtil.KEY);
        } catch (Exception e) {
            e.printStackTrace();
        }

        List<DelayBean> delayBeans = dao.select(delayBean);
        if(delayBeans.size()==0&&userId!=null){
            ClazzUserBean clazzUserBean = new ClazzUserBean();
            clazzUserBean.setUserId(Integer.parseInt(userId));
            clazzUserBean = clazzUserDao.selectOne(clazzUserBean);
            delayBean.setClazzId(clazzUserBean.getClazzId());

            clazzUserBean.setId(null);
            clazzUserBean.setUserId(null);
            clazzUserBean.setType(0);
            clazzUserBean = clazzUserDao.selectOne(clazzUserBean);
            delayBean.setTeacherId(clazzUserBean.getUserId());

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date  =df.format(new Date());
            delayBean.setCreateTime(date);
            delayBean.setLastUpdateTime(date);

            int counter = dao.insert(delayBean);
            return counter;
        }
        return 0;
    }

    @Override
    public int updateDelayRecord(DelayBean delayBean) {

        if(delayBean.getUserId()==0||delayBean.getDelayDate()==""){
            return 0;
        }
        Integer id = delayBean.getId();
        delayBean.setId(null);

        List<DelayBean> delayBeans = dao.select(delayBean);
        if(delayBeans.size()==0){
            delayBean.setId(id);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date  =df.format(new Date());
            delayBean.setLastUpdateTime(date);
            return dao.updateByPrimaryKeySelective(delayBean);
        }
        return 0;
    }

    @Override
    public int deleteDelayRecord(List<Integer> ids) {


        return dao.deleteDelayRecord(ids);
    }

    @Override
    public List<ClazzUserBean> findAllStudentByClazzId(Integer userId) {
        ClazzUserBean clazzUserBean = new ClazzUserBean();
        clazzUserBean.setUserId(userId);
        ClazzUserBean bean = clazzUserDao.selectOne(clazzUserBean);
        if(bean==null){
            return null;
        }
        return clazzUserDao.findAllStudentByClazzId(bean);
    }

    @Override
    public List<DelayBean> findAllDelayRecord(Integer userId) {

        // 结果初始化
        List<DelayBean> delayRecord =null;

        // 入参检测
        if(userId!=null||userId!=0){
            // 得到用户角色编码

            String roleCode = getRoleCodeByUserId(userId);

            DelayBean delayBean =new DelayBean();
            if("class-administrator".equals(roleCode)){
                // 班级管理员
                ClazzUserBean clazzUserBean = new ClazzUserBean();
                clazzUserBean.setUserId(userId);
                ClazzUserBean bean = clazzUserDao.selectOne(clazzUserBean);
                delayBean.setClazzId(bean.getClazzId());
            }else if("student".equals(roleCode)){

                // 学生
                delayBean.setUserId(userId);
            }else if("teacher".equals(roleCode)){

                // 老师
                delayBean.setTeacherId(userId);
            }



            delayRecord =dao.findAllDelayRecord(delayBean);

        }


        return delayRecord;
    }

    @Override
    public List<DelayBean> findDelayRecord(Integer userId,Integer begin, Integer end) {

        // 结果初始化
        List<DelayBean> delayRecord =null;

        // 入参检测
        if(userId!=null||userId!=0){
            // 得到用户角色编码

            String roleCode = getRoleCodeByUserId(userId);

            DelayBean delayBean =new DelayBean();
            if("class-administrator".equals(roleCode)){
                // 班级管理员
                ClazzUserBean clazzUserBean = new ClazzUserBean();
                clazzUserBean.setUserId(userId);
                ClazzUserBean bean = clazzUserDao.selectOne(clazzUserBean);
                delayBean.setClazzId(bean.getClazzId());
            }else if("student".equals(roleCode)){

                // 学生
                delayBean.setUserId(userId);
            }else if("teacher".equals(roleCode)){

                // 老师
                delayBean.setTeacherId(userId);
            }



            delayRecord = dao.findDelayRecord(delayBean,begin, end);

        }





        return delayRecord;
    }

    @Override
    public String getRoleCodeByUserId(Integer userId) {
        // 得到角色ID
        UserRoleBean userRoleBean = new UserRoleBean();
        userRoleBean.setUserId(userId);
        userRoleBean = userRoleDao.selectOne(userRoleBean);
        Integer roleId = userRoleBean.getRoleId();

        // 得到角色编码
        RoleBean roleBean = new RoleBean();
        roleBean.setId(roleId);
        roleBean = roleDao.selectOne(roleBean);
        return  roleBean.getRoleCode();
    }

    @Override
    public List<DelayBean> findAllGroup(Integer userId) {
        // 结果初始化
        List<DelayBean> delayRecord =null;

        // 入参检测
        if(userId!=null||userId!=0){

            // 得到用户角色编码

            String roleCode = getRoleCodeByUserId(userId);

            DelayBean delayBean =new DelayBean();

            if("teacher".equals(roleCode)){
                // 老师
                delayBean.setTeacherId(userId);
                delayRecord = dao.findAllGroup(delayBean);
            }


        }

        return delayRecord;
    }
}
